GBML manual / Maths

square_int

The same as square, for a whole number: value times itself - square_int(5) is 25 - and the answer is a whole number. Use this one for something counted, such as an area in tiles or a squared distance in whole pixels, so the variable you keep it in stays a whole number. Numbers bigger than about 46,340 go wrong, because their square no longer fits.

Syntax

square_int(value) -> int

Arguments

NameType
valueint

Returns

int

Description

The same as square, for a whole number: value times itself - and the answer is a whole number. square_int(5) is 25, and square_int(-3) is 9 - a minus times a minus is a plus, so the answer is never negative.

Reach for this one whenever the number is counted rather than measured: an area in tiles, a number of cells in a square grid, a squared distance in whole pixels. Use square for things that are measured, or when the number has a fraction.

Example

var side = 12;
var area = square_int(side);

area is 144, and it is a whole number, so it can be handed to anything that wants one.

Why there are two of these

square and square_int do the same arithmetic. They differ in the type of the answer, and that difference is worth a function of its own because of what goes wrong without it.

square hands back a real. So this line:

area = square(area + 1);          <- the trap: this is NOT what you want here

turns area into a real - the value is still a whole number, but the kind of number has changed. Nothing complains there. The complaint arrives later, somewhere else, wherever area is next handed to something that wants a whole number, such as the frame of draw_sprite, which says which picture of a sprite to show:

Argument 4 to `draw_sprite` should be an int, but this is a real.

That error points at a line that looks perfectly fine, and it may be in a different file from the assignment that caused it. Written with square_int, it cannot happen.

The number going in has to be whole

square_int takes a whole number only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked:

Argument 1 to `square_int` should be an int, but this is a real.

That is what you get from square_int(2.5), or from square_int(x), because x is a real. If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round. If the number really is measured, use square instead.

Keep the number small - but not as small as with square

A whole number has far more room than a real. square(725) has already gone wrong (a real runs out at about 524,000), but square_int(725) is 525,625, and it stays right all the way up to square_int(46340), which is 2,147,395,600.

Past that the answer wraps round: square_int(46341) comes out as -2,147,479,015 where it should be 2,147,488,281. There is no error and no warning - the answer is simply wrong, the same as for any other whole-number sum that grows past its edge.

If what you want is the distance between two points, dist_to is the safer way: it squares whole numbers of tiny steps and takes the square root for you.

Cost

One multiply - cheap enough for every frame.

On the GBA

square and square_int differ in TYPE, not in arithmetic. square hands back a real, so `area = square(side)` turns a whole number into a real one, and the next place that wants a whole number refuses it - usually somewhere else entirely, which makes it a puzzle. square_int hands back a whole number, so it cannot happen. The number going in has to be whole: square_int(x) is refused when the project is checked because x is a real - round it first with int_floor, int_ceil or int_round. A whole number has far more room than a real: square(725) has already wrapped round, but square_int(725) is 525,625, and it stays right up to square_int(46340), which is 2,147,395,600. square_int(46341) comes out as -2,147,479,015 instead of 2,147,488,281, silently, with no error. A negative value is fine: square_int(-3) is 9. One multiply, cheap enough for every frame. For the distance between two points, use dist_to.

See also: squarepower_intdist_toint_floor